package com.xjtu.mybatisplus.web.controller.login;

import com.xjtu.mybatisplus.common.JsonResponse;
//import com.xjtu.mybatisplus.common.Result;
import com.xjtu.mybatisplus.model.dto.UserLoginDto;
import com.xjtu.mybatisplus.model.vo.UserLoginVo;
import com.xjtu.mybatisplus.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api")
@Slf4j
@Api(tags = "登录接口")
public class LoginController {
    @Autowired
    private UserService userService;

    @PostMapping("user/login")
    @ApiOperation("普通用户登录")
    private JsonResponse<UserLoginVo> userLogin(@RequestBody UserLoginDto userLoginDto) {

        UserLoginVo userLoginVo = userService.userLogin(userLoginDto);

        if(userLoginVo == null) return JsonResponse.failure("用户名或密码错误");

        log.info("当前登录用户为{}",userLoginDto);
        return JsonResponse.success(userLoginVo);
    }

    @PostMapping("admin/login")
    @ApiOperation("后台用户登录")
    private JsonResponse<UserLoginVo> adminLogin(@RequestBody UserLoginDto userLoginDto) {

        UserLoginVo userLoginVo = userService.adminLogin(userLoginDto);

        if(userLoginVo == null) return JsonResponse.failure("用户不存在");

        log.info("当前登录用户为{}",userLoginDto);
        return JsonResponse.success(userLoginVo);
    }











}
